<template>
  <div class="my_edit_wrap my-fixed">
    <div class="parent">
      <div class="m-no-select cursor_default edit_t_w">
        <h3 style='display: inline-block;'>个人信息修改</h3>
        <img src="static/image/home/closeWindow.png" @click="closeEdit" />
      </div>
      <div class="m_edit_main">
        <div>
          <div>账号</div>
          <Input disabled :value="myInfo.id||myInfo.ID||'--'" />
        </div>
        <div>
          <div>工号</div>
          <Input disabled :value="myInfo.employeeno||'--'" />
        </div>
        <div>
          <div>性别</div>
          <Input disabled :value="myInfo.gender==2?'女':'男'" />
        </div>
        <div>
          <div>部门</div>
          <Input disabled :value="$isNull(myInfo.departmentname)?'--':myInfo.departmentname" />
        </div>
        <div>
          <div>职务</div>
          <Input disabled :value="$isNull(myInfo.jobtitle)?'--':myInfo.jobtitle" />
        </div>
        <div>
          <div>手机号</div>
          <Input v-model='myMobile' />
        </div>
        <div>
          <div>电话</div>
          <Input v-model='myTelephone' />
        </div>
        <div>
          <div>邮箱</div>
          <Input v-model='myEmail' />
        </div>
        <div>
          <div></div>
          <div class="edit_btn_wrap">
            <button class="determine_btn" @click="eidtDetermine">确定</button>
            <button class="default_btn" @click="closeEdit">取消</button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { updateUserInfo } from "@/api/editInfo";
// type:1 广播 2:常用联系人 3：我的群组 4：组织架构
import { mapState } from "vuex";
export default {
  props: ["Mobile", "Email", "Telephone"],
  data() {
    return {
      myMobile: "",
      myEmail: "",
      myTelephone: "",
    };
  },
  watch: {
    Mobile() {
      if (this.Mobile) {
        this.myMobile = this.Mobile;
      }
    },
    Email() {
      if (this.Email) {
        this.myEmail = this.Email;
      }
    },
    Telephone() {
      if (this.Telephone) {
        this.myTelephone = this.Telephone;
      }
    },
  },
  computed: {
    ...mapState(["myInfo"]),
  },
  mounted() {},
  methods: {
    closeEdit() {
      this.$emit("closeEdit");
    },
    eidtDetermine() {
      if (
        this.myMobile != "" &&
        this.myMobile.replace(/\s+/g, "") != "" &&
        this.myMobile != "--"
      ) {
        let mobileReg =
          /^(13[0-9]|14[5-9]|15[012356789]|166|17[0-8]|18[0-9]|19[8-9])[0-9]{8}$/;
        if (!mobileReg.test(this.myMobile)) {
          this.$Message.info("请输入正确手机号码!");
          return;
        }
      }

      if (
        this.myTelephone != "" &&
        this.myTelephone.replace(/\s+/g, "") != "" &&
        this.myTelephone != "--"
      ) {
        let telephoneReg = /0\d{2,3}-?[0-9]{7,8}$/;
        if (!telephoneReg.test(this.myTelephone)) {
          this.$Message.info("请输入正确的座机号码!");
          return;
        }
      }

      if (
        this.myEmail != "" &&
        this.myEmail.replace(/\s+/g, "") != "" &&
        this.myEmail != "--"
      ) {
        let emailReg =
          /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
        if (!emailReg.test(this.myEmail)) {
          this.$Message.info("请输入正确邮箱!");
          return;
        }
      }
      updateUserInfo({
        id: this.myInfo.id,
        mobile:
          this.myMobile.replace(/\s+/g, "") != ""
            ? this.myMobile.replace(/\s+/g, "")
            : "",
        email:
          this.myEmail.replace(/\s+/g, "") != ""
            ? this.myEmail.replace(/\s+/g, "")
            : "--",
        telephone:
          this.myTelephone.replace(/\s+/g, "") != ""
            ? this.myTelephone.replace(/\s+/g, "")
            : "--",
      })
        .then((res) => {
          if (res.result == 200) {
            this.myInfo.mobile = this.myMobile;
            this.myInfo.email = this.myEmail;
            this.$emit("updateInfo");
            this.$emit("closeEdit");
            this.$Message.info("修改个人信息成功!");
            this.$forceUpdate();
          } else {
            this.$Message.info(res.msg);
          }
        })
        .catch((error) => {
          this.$Message.info("服务器出错,请稍后再试!23");
        });
    },
  },
  components: {},
  created() {},
};
</script>